package com.ayoka.parenting.ui.launch;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.databinding.ActivityLaunchBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.ayoka.parenting.ui.login.LoginByVerifyCodeActivity;
import com.ayoka.parenting.ui.main.MainActivity;
import com.jincp.common.http.HttpProcessor;
import com.jincp.common.utils.GsonUtil;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: LaunchActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class LaunchActivity extends MVPBaseActivity<ActivityLaunchBinding, LaunchContract.View, LaunchPresenter> implements LaunchContract.View {

    private boolean isRefreshTokenSuccess;

    @Override
    protected void onBeforeSetContentLayout() {
        setTheme(R.style.DayTheme);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_launch;
    }

    @Override
    protected void initViews() {
        mDataBinding.skipBtn.setOnClickListener(this);
        mDataBinding.skipBtn.setEnabled(false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (!isTaskRoot()) {
            return;
        }
        if (mPresenter != null) {
            mPresenter.cleanCache();
            if (UserManager.getInstance() == null) {
                mPresenter.startCountDownTask();
                mDataBinding.skipBtn.setEnabled(true);
            } else {
                mPresenter.refreshToken();
            }
        }
    }

    @Override
    protected LaunchPresenter getPresenter() {
        return new LaunchPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.skipBtn) {
            if (mPresenter != null) {
                mPresenter.stopCountDownTask();
            }
        }
    }


    @Override
    public void onRefreshTokenCallback(boolean isSuccess) {
        this.isRefreshTokenSuccess = isSuccess;
        if (mPresenter != null) {
            mPresenter.startCountDownTask();
            mDataBinding.skipBtn.setEnabled(true);
        }
    }

    @Override
    public void onCountDownTicktock(int seconds) {
        mDataBinding.skipBtn.setText(seconds + "跳过");
    }

    @Override
    public void onCountDownCompleted() {
        if (isRefreshTokenSuccess) {
            if (mPresenter != null) {
                mPresenter.loginTIC();
            }
        } else  {
            HttpProcessor.getInstance().cancelRequest(mPresenter);
            LoginByVerifyCodeActivity.start(LaunchActivity.this);
            finish();
        }
    }

    @Override
    public void onLoginTICCallback(boolean isSuccess) {
        if (isSuccess) {
            MainActivity.start(LaunchActivity.this);
            finish();
        } else {
            LoginByVerifyCodeActivity.start(LaunchActivity.this);
            finish();
        }
    }
}
